package commands;

import java.util.Collection;
import java.util.HashMap;
import java.util.Set;

import main.InputHandler;
import util.*;

/*
 * Command used to define a instance variable and store it to memory
 */
public class DefineCommand extends Command{
	public static String name = "define";
	public static String help = "Name: define \n>define {str:opt('str','int','float')} {str} = {obj} \n" +
			">define (value type) (name) = (value) \n>Command for creating and saving a variable, either a string, int, or float.";
	private static HashMap<String, Object> vars = new HashMap<String, Object>();
	private static String valName;
	private static String curVal;
	
	public static boolean test(InputHandler handler, StringList words){
		if(StringChecker.testWord(words, 0, "define")){
			String[] varTypes = {"str", "int", "float"};
			int choice = StringChecker.testWord(words, 1, varTypes);
			if(choice == -1){
				handler.terminal.write("Invalid variable type");
				return true;
			}
			if(words.size() > 2 && words.get(2) != "null"){
				valName = words.get(2);
			}else{
				handler.terminal.write("Couldn't create variable");
				return true;
			}
			if(!(words.size() > 3 && words.get(3).equals("="))){
				handler.terminal.write("Couldn't create variable");
				return true;
			}
			if(words.size() > 4){
				curVal = words.get(4);
			}else{
				handler.terminal.write("No value provided");
				return true;
			}
			switch(choice){
			case 0:
				vars.put(valName, curVal);
				break;
			case 1:
				vars.put(valName, Integer.parseInt(curVal));
				break;
			case 2:
				vars.put(valName, Float.parseFloat(curVal));
				break;
			}
			return true;
		}
		return false;
	}
	
	public static boolean containsKey(String key){
		return vars.containsKey(key);
	}
	
	public static boolean remove(String key){
		return vars.remove(key) != null;
	}
	
	public static Object get(String key){
		return vars.get(key);
	}
	
	public static Collection<Object> getValues(){
		return vars.values();
	}
	
	public static Set<String> getKeys(){
		return vars.keySet();
	}
	
}
